package com.lanyou.cook.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonObject;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.SystemSource;
import com.lanyou.cook.repository.SystemSourceRepository;

@RestController
@RequestMapping(Constants.BASE_URL_API + "/systemSources")
public class SystemSourceController{
	
	@Autowired
	private SystemSourceRepository systemSourceRepository;
	
	@RequestMapping(value = "/search/checkIsCanSave", produces = "application/json")
	public String checkIsCanSave(String name,String originalId) {
		SystemSource systemSource = systemSourceRepository.findByName(name);
			if (systemSource != null) {
			if (!StringUtils.equals(systemSource.getId(), originalId)) {
				JsonObject result = new JsonObject();
				result.addProperty("isCanSave", false);
				result.addProperty("errorCode", "DUPLICATE");
				return result.toString();
			}
		}

		JsonObject result = new JsonObject();
		result.addProperty("isCanSave", true);
		return result.toString();
	}
	
	
}